package com.adobe.webapis.odeo
{
	import com.adobe.webapis.URLLoaderBase;
	import com.adobe.webapis.odeo.events.OdeoResultEvent;
	
	import flash.net.URLRequest;
	
	import flash.events.Event;
	import mx.utils.StringUtil;
	import com.adobe.utils.DateUtil;
	import com.adobe.net.DynamicURLLoader;

	import com.adobe.webapis.odeo.Podcast;


		/**
		* 	Broadcast when podcast information has been loaded from Odeo in response to
		* 	searchTag() being called.
		*
		* 	The event contains the following properties:		
		* 	data.podcasts An Array of Podcast ojects.
		*	data.searchTerm The tag that was specifed to generate the result 	
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 8.5
		*	@tiptext
		*   
		*	@eventType com.adobe.webapis.odeo.events.OdeoResultEvent.ON_SEARCH_TAG_RESULT
		* 
		* 	@see #searchTag
		* 	@see #com.adobe.webapis.odeo.Podcast
		*/
		[Event(name="onSearchTagResult", 
			 type="com.adobe.webapis.odeo.events.OdeoResultEvent")]			
		
		/**
		* 	Broadcast when podcast information has been loaded from Odeo in response to
		* 	searchUser() being called.
		*
		* 	The event contains the following properties:		
		* 	data.podcasts An Array of Podcast ojects.	
		*	data.searchTerm The username that was specifed to generate the result 
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 8.5
		*	@tiptext
		* 
		*	@eventType com.adobe.webapis.odeo.events.OdeoResultEvent.ON_SEARCH_USER_RESULT 
		* 
		* 	@see #searchUser
		* 	@see #com.adobe.webapis.odeo.Podcast
		*/
		[Event(name="onSearchUserResult", 
			 type="com.adobe.webapis.odeo.events.OdeoResultEvent")]

	/**
	*	Class that provides an ActionScript 3 API to the Odeo Podcasting site.
	*
	* 	@langversion ActionScript 3.0
	*	@playerversion Flash 8.5
	*	@tiptext
	* 
	*	@author Mike Chambers, Adobe Systems Incorporated
	* 
	* 	@see http://www.odeo.com	
	*/
	public class OdeoService extends URLLoaderBase 
	{			
		private var tagSearchURL:String = "http://odeo.com/tag/{0}/rss.xml";
		private var userSearchURL:String = "http://odeo.com/profile/{0}/rss";
		
		private var itunes:Namespace = new Namespace("http://www.itunes.com/DTDs/Podcast-1.0.dtd");
		
		/**
		*	Constructor for OdeoService class.
		*
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 8.5
		*	@tiptext
		*/		
		public function OdeoService()
		{
			super();
		}

		/**
		* 	Retreieves information about podcast that are tagged with the specified
		*	term.
		* 
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 8.5
		*	@tiptext
		* 
		* 	@param searchTerm The tag to search for.
		*/ 
		public function searchTag(searchTerm:String):void
		{
			var url:String = StringUtil.substitute(tagSearchURL, escape(searchTerm));
			
			var loader:DynamicURLLoader = getURLLoader();
				loader.addEventListener(Event.COMPLETE, onSearchTagResult);
				//need to listen for errors
				
			var request:URLRequest = new URLRequest();
				request.url = url;	
				
				loader.searchTerm = searchTerm;
				loader.load(request);
		}
		
		/**
		* 	Retreieves information about podcast that are within the specified
		 * 	user's Audio Inbox.
		* 
		* 	@langversion ActionScript 3.0
		*	@playerversion Flash 8.5
		*	@tiptext
		* 
		* 	@param userName The Odeo username whose podcasts will be retrieved.
		*/ 
		public function searchUser(userName:String):void
		{
			var url:String = StringUtil.substitute(userSearchURL, escape(userName));
			
			var loader:DynamicURLLoader = getURLLoader();
				loader.addEventListener(Event.COMPLETE, onSearchUserResult);
				//need to listen for errors
				
			var request:URLRequest = new URLRequest();
				request.url = url;	
				
				loader.searchTerm = userName;
				loader.load(request);			
		}
		
		private function onSearchTagResult(event:Event):void
		{
			var r:OdeoResultEvent = 
						new OdeoResultEvent(OdeoResultEvent.ON_SEARCH_TAG_RESULT);
						
				r.data.podcasts = parseFeed(new XML(event.target.data));
				r.data.searchTerm = event.target.searchTerm;
				
			dispatchEvent(r);
		}
		
		private function onSearchUserResult(event:Event):void
		{
			var r:OdeoResultEvent = 
						new OdeoResultEvent(OdeoResultEvent.ON_SEARCH_USER_RESULT);
						
				r.data.podcasts = parseFeed(new XML(event.target.data));
				r.data.searchTerm = event.target.searchTerm;
				
			dispatchEvent(r);			
		}
		
		public function parseFeed(rss:XML):Array
		{
			var items:XMLList = rss..item;
			
			var len:int = items.length();
			
			var out:Array = new Array();
			var pItem:Podcast;
			var item:XML;
			
			for(var i:int = 0; i < len; i++)
			{
				item = items[i];
				pItem = new Podcast();			
				
				pItem.author = String(item.itunes::author);
				pItem.keywords = String(item.itunes::keywords).split(" ");
				pItem.size = Number(item.enclosure.@length);
				pItem.publishDate = DateUtil.parseRFC822(String(item.pubDate));
				pItem.title = String(item.title);
				pItem.contentType = String(item.enclosure.@type);
				pItem.url = String(item.enclosure.@url);
				pItem.description = String(item.description);
				pItem.link = String(item.link);

				out.push(pItem);
			}
			
			return out;
		}
	}
}